package br.com.pizzaria.util.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import br.com.pizzaria.model.Cliente;
import br.com.pizzaria.model.dao.ClienteDAO;

@FacesConverter(value = "clienteConverter")
public class ClienteConverter implements Converter {

	private Cliente cliente = new Cliente();

	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		if (value != null && !value.equals("")) {
			ClienteDAO dao = new ClienteDAO();
			cliente.setIdCliente(Integer.valueOf(value));
			return dao.consultar(cliente);
		}
		return null;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
		if (value != null && !value.equals("")) {
			Cliente cliente = (Cliente) value;
			return cliente.getIdCliente() + "";
		}
		return "";
	}

}
